home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / CBOPS.C < prev    next >
Text File  |  1991-09-23  |  5KB  |  246 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbops.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDDEF
  11. #include <stddef.h>
  12. #endif
  13. #ifdef AC_STDLIB
  14. #include <stdlib.h>
  15. #endif
  16. #ifdef AC_STRING
  17. #include <string.h>
  18. #endif
  19.  
  20. /* non-ansi headers */
  21. #include <bool.h>
  22.  
  23. /* library headers */
  24. #include <blkio.h>
  25. #include <btree.h>
  26.  
  27. /* local headers */
  28. #include "cbase_.h"
  29.  
  30. /*man---------------------------------------------------------------------------
  31. NAME
  32.      cb_alloc - allocate memory for cbase
  33.  
  34. SYNOPSIS
  35.      #include "cbase_.h"
  36.  
  37.      int cb_alloc(cbp);
  38.      cbase_t *cbp;
  39.  
  40. DESCRIPTION
  41.      The cb_alloc function allocates the memory needed by cbp.  The
  42.      memory is is initialized to all zeros.
  43.  
  44.      cb_alloc will fail if one or more of the following is true:
  45.  
  46.      [EINVAL]       cbp is not a valid cbase pointer.
  47.      [ENOMEM]       Not enough memory is available for
  48.                     allocation by the calling process.
  49.      [CBENOPEN]     cbp is not open.
  50.  
  51. SEE ALSO
  52.      cb_freemem.
  53.  
  54. DIAGNOSTICS
  55.      Upon successful completion, a value of 0 is returned.  Otherwise,
  56.      a value of -1 is returned, and errno set to indicate the error.
  57.  
  58. ------------------------------------------------------------------------------*/
  59. #ifdef AC_PROTO
  60. int cb_alloc(cbase_t *cbp)
  61. #else
  62. int cb_alloc(cbp)
  63. cbase_t *cbp;
  64. #endif
  65. {
  66. #ifdef DEBUG
  67.     /* validate arguments */
  68.     if (!cb_valid(cbp)) {
  69.         CBEPRINT;
  70.         errno = EINVAL;
  71.         return -1;
  72.     }
  73.  
  74.     /* check if not open */
  75.     if (!(cbp->flags & CBOPEN)) {
  76.         CBEPRINT;
  77.         errno = CBENOPEN;
  78.         return -1;
  79.     }
  80.  
  81.     /* check for memory leak */
  82.     if (cbp->fldv != NULL || cbp->btpv != NULL) {
  83.         CBEPRINT;
  84.         errno = CBEPANIC;
  85.         return -1;
  86.     }
  87. #endif
  88.     /* field definition array */
  89.     cbp->fldv = (cbfield_t *)calloc((size_t)cbp->fldc, sizeof(*cbp->fldv));
  90.     if (cbp->fldv == NULL) {
  91.         CBEPRINT;
  92.         errno = ENOMEM;
  93.         return -1;
  94.     }
  95.  
  96.     /* btree array */
  97.     cbp->btpv = (btree_t **)calloc((size_t)cbp->fldc, sizeof(*cbp->btpv));
  98.     if (cbp->btpv == NULL) {
  99.         CBEPRINT;
  100.         free(cbp->fldv);
  101.         errno = ENOMEM;
  102.         return -1;
  103.     }
  104.  
  105.     return 0;
  106. }
  107.  
  108. /*man---------------------------------------------------------------------------
  109. NAME
  110.      cb_freemem - free memory allocated for cbase
  111.  
  112. SYNOPSIS
  113.      #include "cbase_.h"
  114.  
  115.      void cb_freemem(cbp)
  116.      cbase_t *cbp;
  117.  
  118. DESCRIPTION
  119.      The cb_freemem function frees all memory allocated for cbase cbp.
  120.      If cbp is not a valid cbase, no action is taken.
  121.  
  122. SEE ALSO
  123.      cb_alloc.
  124.  
  125. ------------------------------------------------------------------------------*/
  126. #ifdef AC_PROTO
  127. void cb_freemem(cbase_t *cbp)
  128. #else
  129. void cb_freemem(cbp)
  130. cbase_t *cbp;
  131. #endif
  132. {
  133. #ifdef DEBUG
  134.     /* validate arguments */
  135.     if (!cb_valid(cbp)) {
  136.         CBEPRINT;
  137.         return;
  138.     }
  139. #endif
  140.     /* free memory */
  141.     if (cbp->fldv != NULL) {
  142.         free(cbp->fldv);
  143.         cbp->fldv = NULL;
  144.     }
  145.     if (cbp->btpv != NULL) {
  146.         free(cbp->btpv);
  147.         cbp->btpv = NULL;
  148.     }
  149.  
  150.     return;
  151. }
  152.  
  153. /*man---------------------------------------------------------------------------
  154. NAME
  155.      cb_fvalid - validate field definition list
  156.  
  157. SYNOPSIS
  158.      #include "cbase_.h"
  159.  
  160.      bool cb_fvalid(recsize, fldc, fldv)
  161.      size_t recsize;
  162.      int fldc;
  163.      const cbfield_t fldv[];
  164.  
  165. DESCRIPTION
  166.      The cb_fvalid function determines if fldc and fldv constitute a
  167.      valid field definition list for a cbase with records of size
  168.      recsize.  If valid, then TRUE is returned.  If not, then FALSE is
  169.      returned.
  170.  
  171. ------------------------------------------------------------------------------*/
  172. #ifdef AC_PROTO
  173. bool cb_fvalid(size_t recsize, int fldc, const cbfield_t fldv[])
  174. #else
  175. bool cb_fvalid(recsize, fldc, fldv)
  176. size_t recsize;
  177. int fldc;
  178. const cbfield_t fldv[];
  179. #endif
  180. {
  181.     size_t    end    = 0;
  182.     int    i    = 0;
  183.  
  184.     if (recsize < sizeof(bpos_t) || fldc < 1 || fldv == NULL) {
  185.         return FALSE;
  186.     }
  187.  
  188.     for (i = 0; i < fldc; ++i) {
  189.         if (fldv[i].offset < end) {
  190.             return FALSE;
  191.         }
  192.         if (fldv[i].len < 1) {
  193.             return FALSE;
  194.         }
  195.         end = fldv[i].offset + fldv[i].len;
  196.         if (end > recsize) {
  197.             return FALSE;
  198.         }
  199.         if (fldv[i].type < 0 || fldv[i].type >= CBTYPECNT) {
  200.             return FALSE;
  201.         }
  202.         if (fldv[i].flags & ~CB_FFLAGS) {
  203.             return FALSE;
  204.         }
  205.         if (fldv[i].flags & CB_FKEY) {
  206.             if (fldv[i].filename[0] == NUL) {
  207.                 return FALSE;
  208.             }
  209.         }
  210.     }
  211.  
  212.     return TRUE;
  213. }
  214.  
  215. /*man---------------------------------------------------------------------------
  216. NAME
  217.      cb_valid - validate cbase pointer
  218.  
  219. SYNOPSIS
  220.      #include "cbase_.h"
  221.  
  222.      bool cb_valid(cbp)
  223.      cbase_t *cbp;
  224.  
  225. DESCRIPTION
  226.      The cb_valid function determines if cbp is a valid cbase pointer.
  227.      If valid, then TRUE is returned.  If not, then FALSE is returned.
  228.  
  229. ------------------------------------------------------------------------------*/
  230. #ifdef AC_PROTO
  231. bool cb_valid(cbase_t *cbp)
  232. #else
  233. bool cb_valid(cbp)
  234. cbase_t *cbp;
  235. #endif
  236. {
  237.     if (cbp < cbb || cbp > (cbb + CBOPEN_MAX - 1)) {
  238.         return FALSE;
  239.     }
  240.     if ((((char *)cbp - (char *)cbb)) % sizeof(*cbb) != 0) {
  241.         return FALSE;
  242.     }
  243.  
  244.     return TRUE;
  245. }
  246.